<?php
namespace addons\hellozjx_mapwxapp\inc\page;

trait Teacher
{
	/**
	 * 老师入驻加盟
	 * @return [type] [description]
	 */
	public function doPageEnter()
	{
		$data = array(
			'name' => self::$_GPC['name'], 						// 教师姓名
			'sex' => self::$_GPC['sex'], 						// 性别
			'teach_age' => self::$_GPC['teach_age'], 			// 教龄
			'age' => self::$_GPC['age'], 						// 年龄
			'email' => self::$_GPC['email'],					// 邮箱
			'phone' => self::$_GPC['phone'], 					// 手机号
			'id_card' => self::$_GPC['id_card'], 				// 身份证号
			'qualification' => self::$_GPC['qualification'], 	// 有无职业证书
			'avatar' => self::$_GPC['avatar'], 					// 教师头像
			'sort' => 1, 										// 默认排序
			'status' => 2,										// 默认未审核
			'create_time' => date('Y-m-d H:i:s'),				// 入驻时间
			'city' => self::$_GPC['city'],						// 城市	
			'address' => self::$_GPC['address'],				// 城市	
			'longitude' => self::$_GPC['longitude'],			// 经度	
			'latitude' => self::$_GPC['latitude'],				// 纬度	
		);
		$teacher = pdo_insert($this->table_teacher, $data);
		if (empty($teacher)) {
			return $this->result(-1, '入驻失败，请稍候在试...', []);
		}
		return $this->result(0, '入驻成功，请等待审核');
	}

	/**
	 * 老师详情页
	 * @return [type] [description]
	 */
	public function doPageTeacherDetail()
	{
		$id = self::$_GPC['id'];	// 视频 id
		// 当前视频详情
		$video = pdo_get($this->table_video, array('id' => $id));
		if (empty($video)) {
			return $this->result(-1, '当前视频不存在', []);
		}

		// 查询老师信息
		$teacher = pdo_get($this->table_teacher, array('id' => $video['teacher_id']));
		// 求出老师所带学科
		$sql = "select b.name from " . tablename($this->table_room_teacher) . " as a left join " . tablename($this->table_subject) . " as b on a.type_id = b.id where a.type = 2 and a.tid = {$teacher['id']} order by b.sort asc";
		$teacher_subject = pdo_fetchall($sql);
		
		// 拼装返回数据
		$data = array(
			'teacher_name' => $teacher['name'],
			'subject' => $teacher_subject,//implode(' ', $teacher_subject),
			'teach_age' => $teacher['teach_age'],
			'attr' => array('在线课', '作文', '幽默风趣', '中考'),
			'address' => $teacher['address'],
			'phone' => $teacher['phone'],
			'price' => $video['price'],
			'is_charge' => $video['is_charge'],
			'teacher_id' => $video['teacher_id'],
			'id' => $video['id']
		);
		// 查出该老师的所有在线课程
		$uniacid = self::$_W['uniacid'];
		$sql = "select b.img_url, b.name, b.id, b.price, b.is_charge, a.name as teacher_name, a.city from " . tablename($this->table_video) . " as b left join " . tablename($this->table_teacher) . " as a ON b.teacher_id = a.id where b.uniacid = {$uniacid} and b.is_charge < 2 and b.teacher_id = {$teacher['id']}";
		$result = pdo_fetchall($sql);
		$data['videoList'] = $result;
		// TODO 评论先不放着，后期补上
		return $this->result(0, '', $data);
	}

	/**
	 * 我的 -- 老师
	 * @return [type] [description]
	 */
	public function doPageMyTeacher()
	{
		$openid = self::$_W['openid'];
		// 通过 openid 找到对应的老师
		$teacher = pdo_get($this->table_teacher, array('openid' => $openid), array('money', 'id'));
		if (empty($teacher)) {
			return $this->result(0, '', []);
		}
		$id = $teacher['id'];
		$uniacid = self::$_W['uniacid'];
		// 已发布的视频
		$pindex = max(1, intval($_GPC['page']));
    	$psize = 20;
    	$sql = "select a.id, a.name, a.click, a.create_time, a.img_url, b.name as teacher_name from " . tablename($this->table_video) . " as a left join ". tablename($this->table_teacher) ." as b ON a.teacher_id = b.id where a.teacher_id = {$id} and a.uniacid = {$uniacid} and a.status = 2 order by a.create_time desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize;
    	$videoList = pdo_fetchall($sql);
    	if (!empty($videoList)) {
    		foreach ($videoList as $key => $val) {
    			// 求出每个视频的评论量
    			$videoList[$key]['comment_total'] = pdo_fetchcolumn("select count(*) from ". tablename($this->table_comment) ." where vid =" . $val['id']);
    			// 求出视频收藏数
    			$videoList[$key]['collection_total'] = pdo_fetchcolumn("select count(*) from ". tablename($this->table_collection) ." where vid =". $val['id']);
    			$videoList[$key]['date'] = substr($val['create_time'], 0, 10);
    			$videoList[$key]['time'] = substr($val['create_time'], 10);
    		}
    	}
    	
		return $this->result(0, '', array('data' => $videoList, 'money' => $teacher['money']));
	}

	/**
	 * 检测视频是否已经购买
	 * @return [type] [description]
	 */
	public function doPagePayVideo()
	{
		$id = self::$_GPC['id'];
		$openid = self::$_W['openid'];
		$ext = pdo_get($this->table_order, array('openid' => $openid, 'vid' => $id, 'status' => 2));
		if ($ext) {
			return $this->result(0, '成功', []);
		}
		message(error(1, '该视频还未购买，请先购买'), '', 'ajax');
	}

	public function doPageSmVideo()
	{
		$id = self::$_GPC['id'];
		$openid = self::$_W['openid'];
		// 当前老师的上门视频
		$video = pdo_getall($this->table_video, array('teacher_id' => $id, 'is_charge' => 2), array('id', 'price', 'name'));
		return $this->result(0, '成功', $video);
	}

}